import java.util.Arrays;

public class MyStack implements IStack{

    private int[] elem;
    private int useSize;
    private static final int DEFAULT_CAPACITY=10;

    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }

    public MyStack(int k) {
        this.elem = new int[k];
    }

    //入栈
    @Override
    public void push(int x) {
        if (full()) {
            elem= Arrays.copyOf(elem,2*elem.length);
        }
        elem[useSize]=x;
        useSize++;
    }

    //出栈
    @Override
    public int pop() throws EmptyException{
        if (isEmpty()) {
            throw new EmptyException("栈空了!");
        }
        int old=elem[useSize-1];
        useSize--;
        return old;
    }


    @Override
    public int peek() {
        if (isEmpty()) {
            throw new EmptyException("栈空了!");
        }
        return elem[useSize-1];
    }

    @Override
    public int size() {
        return useSize;
    }

    @Override
    public boolean isEmpty() {
        return useSize==0;
    }

    private boolean full() {
        if (useSize==elem.length) {
            return true;
        }
        return false;
    }
}
